---
--类名: HeartWordsView.lua
--创建日期: 2015-10-29
--创建人: 陈小虎
--功能描述: 心话视图
--修改内容：
--修改日期:
--修改人:
--修改备注:
--

local HeartWordsView = class("HeartWordsView",function()
    return display.newLayer("HeartWordsView")
end)

function HeartWordsView:ctor()

	self.mBoxWidth = 180
    self.mBoxHeight = 100

	self.mUINode = cc.uiloader:load(Res.Talk_Bubble)
    self:addChild(self.mUINode)

    self.mContentBox = self.mUINode:getChildByName("Image_1")--ccui.Scale9Sprite:create(InsetsRect,"public/public_talk.png")
    self.mContentBox:setAnchorPoint(cc.p(0,0))

    self.mTextNode = self.mContentBox:getChildByName("Text")

    local boxSize = self.mContentBox:getContentSize()
    local textNodeSize = self.mTextNode:getContentSize()
    self.mBoxWidth = textNodeSize.width

    self.mTextNode:ignoreContentAdaptWithSize(true)
    self.mTextNode:getVirtualRenderer():setMaxLineWidth(self.mBoxWidth)
    self.mTextNode:getVirtualRenderer():setLineBreakWithoutSpace(true)
    --self.mTextNode:setTextAreaSize(cc.size(htWidth,0))

    self.mOffsetWidth = boxSize.width - textNodeSize.width
    self.mOffsetHeight = boxSize.height - textNodeSize.height

    self.mTextNode:setString(" ")

    local fontSize = self.mTextNode:getFontSize()
    local fontName = self.mTextNode:getFontName()

    self._startShowTime = 0
    local touchListener = cc.EventListenerTouchOneByOne:create()
    touchListener:setSwallowTouches(true)
    touchListener:registerScriptHandler(function(touch, event)
        return true
    end, cc.Handler.EVENT_TOUCH_BEGAN)

    touchListener:registerScriptHandler(function(touch, event)
        local iTime = app:getTime() - self._startShowTime
        if iTime >= 0.5 then
            self._startShowTime = app:getTime()
            self:callOver()
        end
    end, cc.Handler.EVENT_TOUCH_ENDED)
    self:getEventDispatcher():addEventListenerWithSceneGraphPriority(touchListener, self)
--[[
    self.mHideText = ccui.Text:create()
    self.mHideText:setFontName(fontName)
    self.mHideText:setFontSize(fontSize)
    self:addChild(self.mHideText)
    self.mHideText:setVisible(false)
    --]]
    -- self.mContentLabel = display.newTTFLabel({
    --     text = " ",
    --     font = fontName,--Res.font,
    --     size = fontSize,
    --     --align = cc.TEXT_ALIGNMENT_CENTER -- 文字内部居中对齐
    --     dimensions = cc.size(self.mBoxWidth, 0)
    -- })
    -- self.mContentLabel:setAnchorPoint(cc.p(0.5,0.5))
    -- self:addChild(self.mContentLabel)
    -- self.mContentLabel:setVisible(false)
end

function HeartWordsView:setCallOver(callback)
	self.mCallback = callback
end

function HeartWordsView:callOver()
    if self.mCallback then
        self.mCallback()
    end
end

function HeartWordsView:setShowDirection(showDir)
	self.mShowDir = showDir-- l左边出现 r右边出现
end

function HeartWordsView:setHoldTime(time)
	self.mHoldTime = time
end

function HeartWordsView:setContent(content)
    -- self.mContentLabel:setString(content)
    -- self.mTextNode:setString(content)

    -- local width = self.mContentLabel:getContentSize().width
    -- local height = self.mContentLabel:getContentSize().height

    -- local bWidth = width + self.mOffsetWidth
    -- local bHeight = height + self.mOffsetHeight

    -- self.mTextNode:setTextAreaSize(cc.size(width,height))
    -- self.mContentBox:setContentSize(cc.size(bWidth,bHeight))
    -- self.mContentBox:setPosition(cc.p(0 - bWidth / 2,self.mContentBox:getPositionY()))
    -- self.mUINode:setContentSize(cc.size(bWidth,bHeight))
    -- ccui.Helper:doLayout(self.mUINode)
--[[
    self.mHideText:setTextAreaSize(cc.size(self.mBoxWidth,0))
    self.mHideText:setString(content)
    local htWidth = self.mHideText:getVirtualRendererSize().width
    if htWidth > self.mBoxWidth then
        local rlen = math.floor(htWidth - self.mBoxWidth)
        htWidth = math.floor(self.mBoxWidth - rlen)
    end


    --self.mTextNode:setTextAreaSize(cc.size(htWidth,0))

    self.mTextNode:setString(content)

    local width = self.mTextNode:getVirtualRendererSize().width
    local height = self.mTextNode:getVirtualRendererSize().height

    local bWidth = self.mBoxWidth + self.mOffsetWidth

    local bHeight = height + self.mOffsetHeight
    --]]
    local outlineSize
    local outlineColor
    if self.mTextNode.getLabelEffectType and self.mTextNode.getOutlineSize and self.mTextNode.getEffectColor then
        local eType = self.mTextNode:getLabelEffectType()
        if eType == 1 then
            outlineSize = self.mTextNode:getOutlineSize()
            outlineColor = self.mTextNode:getEffectColor()
        end
    end

    local textNodeName = "custom_text_node"
    local fontName = self.mTextNode:getFontName()
    local fontSize = self.mTextNode:getFontSize()
    local name = UD:getPlayer().name
    local jobStr = L(GD:queryClass(UD:getPlayer().classID).string)
    local Wildcard = require("app.public.wildcard.Wildcard")
    local wildcard = Wildcard.new()
    local wordsTab = wildcard:parseWildcardStrings(content,fontSize,jobStr,"sex",name)
    local textNode,tWidth,tHeight = wildcard:buildTextNode(wordsTab,self.mBoxWidth,fontName,outlineColor,outlineSize)
    textNode:setName(textNodeName)

    -- 是否添加过Node（主要是为了减少打印错误日志） 
    if self._alreadyAddNode then
        self._alreadyAddNode = false
        self.mContentBox:removeChildByName(textNodeName)
    end

    self.mContentBox:addChild(textNode)
    self._alreadyAddNode = true
    textNode:setPosition(cc.p(self.mOffsetWidth / 2,self.mOffsetHeight / 2 + tHeight))

    local bWidth = tWidth + self.mOffsetWidth
    local bHeight = tHeight + self.mOffsetHeight


    self.mContentBox:setContentSize(cc.size(bWidth,bHeight))
    self.mContentBox:setPosition(cc.p(0 - bWidth / 2,self.mContentBox:getPositionY()))
    self.mUINode:setContentSize(cc.size(bWidth,bHeight))
    ccui.Helper:doLayout(self.mUINode)
end

function HeartWordsView:appear()
	if self.mShowDir == "left" then
		self.mContentBox:setAnchorPoint(cc.p(0,0))
		self.mContentBox:setPosition(cc.p(0,display.height / 2))
	elseif self.mShowDir == "right" then
		self.mContentBox:setAnchorPoint(cc.p(1,0))
		self.mContentBox:setPosition(cc.p(display.width,display.height / 2))
    elseif self.mShowDir == "down1" then
        self.mContentBox:setAnchorPoint(cc.p(0.5, 0))
        self.mContentBox:setPosition(cc.p(display.width * 0.75, 10))
    elseif self.mShowDir == "down2" then
        self.mContentBox:setAnchorPoint(cc.p(0.5, 0))
        self.mContentBox:setPosition(cc.p(display.width * 0.25, 10))
	end

	self.mContentBox:setScale(0)

	local function call()
		self:callOver()
	end

 -- local action = cc.Sequence:create(
 --        cc.ScaleTo:create(0.2,1.2),
 --        cc.ScaleTo:create(0.1,0.9),
 --        cc.ScaleTo:create(0.1,1.0),
 --        cc.DelayTime:create(self.mHoldTime),
 --        cc.CallFunc:create(call))
 --    self.mContentBox:runAction(action)


    local action = cc.CSLoader:createTimeline(Res.Talk_Bubble)
    self.mUINode:runAction(action)
    action:gotoFrameAndPlay(0,action:getDuration(),false)
    action:setLastFrameCallFunc(function()
        if self.mHoldTime then
            local action1 = cc.Sequence:create(
            cc.DelayTime:create(self.mHoldTime),
            cc.CallFunc:create(call))
            self.mContentBox:runAction(action1)
        end
    end)
end

return HeartWordsView
